package com.crazyauntzhang.easyshopbackend.controller;

import com.crazyauntzhang.easyshopbackend.domain.User;
import com.crazyauntzhang.easyshopbackend.request.AddUserResquest;
import com.crazyauntzhang.easyshopbackend.request.DismissUserRequest;
import com.crazyauntzhang.easyshopbackend.request.EditUserResquest;
import com.crazyauntzhang.easyshopbackend.request.FindUserRequest;
import com.crazyauntzhang.easyshopbackend.result.ResponseResult;
import com.crazyauntzhang.easyshopbackend.service.UserService;
import com.crazyauntzhang.easyshopbackend.util.FormatChecker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

@RestController
public class UserController {

	private final UserService userService;

	@Autowired
	public UserController(UserService userService) {
		this.userService = userService;
	}

	/**
	 * 查询所有员工
	 *
	 * @return 包含所有员工对象的响应体
	 */
	@PostMapping("/findAllUsers")
	public ResponseResult<Object> findAllUsers() {
		List<User> userList = userService.findAllUsers();
		if (userList.isEmpty())
			return new ResponseResult<>(10404, "User Not Found");
		return new ResponseResult<>(10200, "OK", userList);
	}

	/**
	 * 按编号或姓名查询员工
	 *
	 * @param findUserRequest 查询用户请求体
	 * @return 包含员工对象的响应体
	 */
	@PostMapping("/findUser")
	public ResponseResult<Object> findUser(@RequestBody FindUserRequest findUserRequest) {
		String userIdOrName = findUserRequest.getUserIdOrName();
		Boolean isOnJob = findUserRequest.getIsOnJob();
		List<User> userList = new ArrayList<>();
		if (FormatChecker.isNumber(userIdOrName)) {
			User user = userService.findUserById(userIdOrName, isOnJob);
			if ((user == null))
				return new ResponseResult<>(10404, "User Not Found");
			userList.add(user);
		} else {
			userList = userService.findUserByName(userIdOrName, isOnJob);
			if (userList.isEmpty()) return new ResponseResult<>(10404, "User Not Found");
		}
		return new ResponseResult<>(10200, "OK", userList);
	}

	/**
	 * 按编号查询员工
	 *
	 * @param findUserRequest 请求体
	 * @return 响应体
	 */
	@Deprecated
	@PostMapping("/findUserById")
	public ResponseResult<Object> findUserById(@RequestBody FindUserRequest findUserRequest) {
		String userID = findUserRequest.getUserID();
		Boolean isOnJob = findUserRequest.getIsOnJob();

		List<User> userList = new ArrayList<>();

		User user = userService.findUserById(userID, isOnJob);
		if ((user == null))
			return new ResponseResult<>(10404, "User Not Found");
		userList.add(user);
		return new ResponseResult<>(10200, "OK", userList);
	}

	/**
	 * 按姓名查询员工
	 *
	 * @param findUserRequest 请求体
	 * @return 响应体
	 */
	@Deprecated
	@PostMapping("/findUserByName")
	public ResponseResult<Object> findUserByName(@RequestBody FindUserRequest findUserRequest) {
		String username = findUserRequest.getUsername();
		Boolean isOnJob = findUserRequest.getIsOnJob();

		List<User> userList = userService.findUserByName(username, isOnJob);
		if (userList.isEmpty())
			return new ResponseResult<>(10404, "User Not Found");
		return new ResponseResult<>(10200, "OK", userList);
	}

	/**
	 * 员工入职
	 *
	 * @param addUserResquest 入职请求体对象
	 * @return 包含入职员工对象的响应体
	 */
	@PostMapping("/addUser")
	public ResponseResult<Object> addUser(@RequestBody AddUserResquest addUserResquest) {
		LocalDate hireDate = LocalDate.now();

		String username = addUserResquest.getUsername();
		String roleID = addUserResquest.getRoleID();
		String tel = addUserResquest.getTel();

		int year = hireDate.getYear();
		int month = hireDate.getMonthValue();
		Integer employeeCount = userService.findEmployeeCount();
		String serial = String.format("%03d", employeeCount + 1);

		String userID = "" + year + month + roleID + serial;

		userService.addUser(userID, username, userID, roleID, Boolean.TRUE, tel, hireDate);

		User user = new User(userID, username, userID, roleID, Boolean.TRUE, tel, hireDate);

		return new ResponseResult<>(10200, "OK", user);
	}

	/**
	 * 修改员工信息
	 *
	 * @param editUserResquest 修改员工信息请求体
	 * @return 响应体
	 */
	@PostMapping("/editUser")
	public ResponseResult<Object> editUser(@RequestBody EditUserResquest editUserResquest) {
		String userID = editUserResquest.getUserID();
		String username = editUserResquest.getUsername();
		String roleID = editUserResquest.getRoleID();
		String tel = editUserResquest.getTel();

		User user = userService.findUserById(userID, Boolean.TRUE);
		if (user == null)
			return new ResponseResult<>(10404, "User Not Found");

		String newUserID = userID.substring(0, 6) + roleID + userID.substring(9, 12);
		userService.changeUser(userID, newUserID, username, user.getPassword(), roleID, user.getIsOnJob(), tel, user.getHireDate());

		User editedUser = new User(newUserID, username, user.getPassword(), roleID, user.getIsOnJob(), tel, user.getHireDate());

		return new ResponseResult<>(10200, "OK", editedUser);
	}

	/**
	 * 解雇员工
	 *
	 * @param dismissUserRequest 解雇员工请求体
	 * @return 响应体
	 */
	@PostMapping("/dismissUser")
	public ResponseResult<Object> dismissUser(@RequestBody DismissUserRequest dismissUserRequest) {
		String userID = dismissUserRequest.getUserID();

		User user = userService.findUserOnJob(userID);
		if (user == null)
			return new ResponseResult<>(10404, "User Nor Found");

		userService.changeUserOffJob(userID);

		return new ResponseResult<>(10200, "OK");
	}

}
